import Taro from '@tarojs/taro';

export default class Router {
    /**
     * basename
     * homepage
     * @param {object} subPackagesReg
     */
    constructor({ subPackagesReg }) {
        this.subPackagesReg = subPackagesReg;
    }

    switchTab = options => {
        return Taro.switchTab(options);
    };
    forward = options => {
        if (options.url.indexOf('http') == 0) {
            setTimeout(function() {
                window.location.href = options.url;
            }, 200);
        } else {
            // 检查分包路由
            if (options.url.match(this.subPackagesReg)) {
                options.url = options.url.replace('/pages', '/base');
            }
            return Taro.navigateTo(options);
        }
    };
    back = options => {
        return Taro.navigateBack(options);
    };
    redirect = options => {
        if (options.url.indexOf('http') == 0) {
            setTimeout(function() {
                window.location.replace(options.url);
            }, 200);
        } else {
            return Taro.redirectTo(options);
        }
    };
}
